package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.comboloader.LocalizedSelect;
import util.enumeration.FnbType;
import util.enumeration.Language;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;


/**
 *
 * @author Amelia
 */
@FacesConverter(value = "fnbTypeConverter", forClass = util.enumeration.FnbType.class)
public class FnbTypeConverter implements Converter {
    
    private WebLocalization localization;
    private String preferredLocale;
    
    public FnbTypeConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {  
        
        preferredLocale = localization.getPreferredLocaleFromSession();

        if (value == null || value.length() == 0) {
            return null;
        }

        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getFnbTypeAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getFnbTypeAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getFnbTypeAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getFnbTypeAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getFnbTypeAsObject_EN_US(value);
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {

        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }

        if (value instanceof FnbType) {
            FnbType fnbType = (FnbType) value;

           String localizedFnbType = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedFnbType = getFnbTypeAsString_EN_US(fnbType);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedFnbType = getFnbTypeAsString_EN_SG(fnbType);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedFnbType = getFnbTypeAsString_MS_MY(fnbType);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedFnbType = getFnbTypeAsString_ZH_CN(fnbType);
            }
            // other language...
            else {
                localizedFnbType = getFnbTypeAsString_EN_US(fnbType);
            }

            return localizedFnbType;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.FnbType");
        }
    }
    
     // simplified chinese (zh_CN)
     private static Object getFnbTypeAsObject_ZH_CN(String value) {
        if(value.equals("小酒馆")) return FnbType.BISTRO;
        else if(value.equals("自助餐")) return FnbType.BUFFET;
        else if(value.equals("咖啡厅")) return FnbType.CAFE;
        else if(value.equals("食堂")) return FnbType.CAFETERIA;
        else if(value.equals("咖啡馆")) return FnbType.COFFEE_HOUSE;
        else if(value.equals("咖啡店")) return FnbType.COFFEE_SHOP;
        else if(value.equals("休闲餐饮式")) return FnbType.CASUAL_DINING;
        else if(value.equals("家庭式")) return FnbType.FAMILY_STYLE;
        else if(value.equals("快速休闲式")) return FnbType.FAST_CASUAL;
        else if(value.equals("快餐")) return FnbType.FAST_FOOD;
        else if(value.equals("精致餐饮式")) return FnbType.FINE_DINING;
        else if(value.equals("美食广场")) return FnbType.FOOD_COURT;
        else if(value.equals("小贩中心")) return FnbType.HAWKER_CENTRE;
        else if(value.equals("酒吧")) return FnbType.PUB;
        else if(value.equals("茶馆")) return FnbType.TEA_HOUSE;
        else return null;
    }
     
     
      // default (en_US)
      private static Object getFnbTypeAsObject_EN_US(String value) {
        if(value.equals("Bistro")) return FnbType.BISTRO;
        else if(value.equals("Buffet")) return FnbType.BUFFET;
        else if(value.equals("Cafe")) return FnbType.CAFE;
        else if(value.equals("Cafeteria")) return FnbType.CAFETERIA;
        else if(value.equals("Coffee House")) return FnbType.COFFEE_HOUSE;
        else if(value.equals("Coffee Shop")) return FnbType.COFFEE_SHOP;
        else if(value.equals("Casual Dining")) return FnbType.CASUAL_DINING;
        else if(value.equals("Family Style")) return FnbType.FAMILY_STYLE;
        else if(value.equals("Fast Casual")) return FnbType.FAST_CASUAL;
        else if(value.equals("Fast Food")) return FnbType.FAST_FOOD;
        else if(value.equals("Fine Dining")) return FnbType.FINE_DINING;
        else if(value.equals("Food Court")) return FnbType.FOOD_COURT;
        else if(value.equals("Hawker Centre")) return FnbType.HAWKER_CENTRE;
        else if(value.equals("Pub")) return FnbType.PUB;
        else if(value.equals("Tea House")) return FnbType.TEA_HOUSE;
        else return null;
    }
    
      // singapore english (en_SG)
      private static Object getFnbTypeAsObject_EN_SG(String value) {
        if(value.equals("Bistro")) return FnbType.BISTRO;
        else if(value.equals("Buffet")) return FnbType.BUFFET;
        else if(value.equals("Cafe")) return FnbType.CAFE;
        else if(value.equals("Cafeteria")) return FnbType.CAFETERIA;
        else if(value.equals("Coffee House")) return FnbType.COFFEE_HOUSE;
        else if(value.equals("Coffee Shop")) return FnbType.COFFEE_SHOP;
        else if(value.equals("Casual Dining")) return FnbType.CASUAL_DINING;
        else if(value.equals("Family Style")) return FnbType.FAMILY_STYLE;
        else if(value.equals("Fast Casual")) return FnbType.FAST_CASUAL;
        else if(value.equals("Fast Food")) return FnbType.FAST_FOOD;
        else if(value.equals("Fine Dining")) return FnbType.FINE_DINING;
        else if(value.equals("Food Court")) return FnbType.FOOD_COURT;
        else if(value.equals("Hawker Centre")) return FnbType.HAWKER_CENTRE;
        else if(value.equals("Pub")) return FnbType.PUB;
        else if(value.equals("Tea House")) return FnbType.TEA_HOUSE;
        else return null;
    }
     
      // bahasa malaysia (ms_MY)
      private static Object getFnbTypeAsObject_MS_MY(String value) {
        if(value.equals("Bistro")) return FnbType.BISTRO;
        else if(value.equals("Bufet")) return FnbType.BUFFET;
        else if(value.equals("Cafe")) return FnbType.CAFE;
        else if(value.equals("Cafeteria")) return FnbType.CAFETERIA;
        else if(value.equals("Rumah Coffee")) return FnbType.COFFEE_HOUSE;
        else if(value.equals("Kedai Kopi")) return FnbType.COFFEE_SHOP;
        else if(value.equals("Santapan Gaya Kasual")) return FnbType.CASUAL_DINING;
        else if(value.equals("Gaya Keluarga")) return FnbType.FAMILY_STYLE;
        else if(value.equals("Fast Kasual")) return FnbType.FAST_CASUAL;
        else if(value.equals("Makanan Segera")) return FnbType.FAST_FOOD;
        else if(value.equals("Santapan")) return FnbType.FINE_DINING;
        else if(value.equals("Mahkamah Makanan")) return FnbType.FOOD_COURT;
        else if(value.equals("Penjaja Pusat")) return FnbType.HAWKER_CENTRE;
        else if(value.equals("Pub")) return FnbType.PUB;
        else if(value.equals("Rumah Teh")) return FnbType.TEA_HOUSE;
        else return null;
    }
    

     // simplified chinese (zh_CN)
    private static String getFnbTypeAsString_ZH_CN(FnbType fnbType) {
        String localizedFnbType = "";
        
        switch (fnbType) {
            case BISTRO: localizedFnbType = "小酒馆" ; break;
            case BUFFET: localizedFnbType = "自助餐"; break;
            case CAFE: localizedFnbType = "咖啡厅"; break;
            case CAFETERIA: localizedFnbType = "食堂"; break;
            case COFFEE_HOUSE: localizedFnbType = "咖啡馆"; break;
            case COFFEE_SHOP: localizedFnbType = "咖啡店"; break;
            case CASUAL_DINING: localizedFnbType = "休闲餐饮式"; break;
            case FAMILY_STYLE: localizedFnbType = "家庭式"; break;
            case FAST_CASUAL: localizedFnbType = "快速休闲式"; break;
            case FAST_FOOD: localizedFnbType = "快餐"; break;
            case FINE_DINING: localizedFnbType = "精致餐饮式"; break;
            case FOOD_COURT: localizedFnbType = "美食广场"; break;
            case HAWKER_CENTRE: localizedFnbType = "小贩中心"; break;
            case PUB: localizedFnbType = "酒吧"; break;
            case TEA_HOUSE: localizedFnbType = "茶馆"; break;
        }
        return localizedFnbType;
    }
    
    // default (en_US)
    private static String getFnbTypeAsString_EN_US(FnbType fnbType) {
        String localizedFnbType = "";
        
        switch (fnbType) {
            case BISTRO: localizedFnbType = "Bistro" ; break;
            case BUFFET: localizedFnbType = "Buffet"; break;
            case CAFE: localizedFnbType = "Cafe"; break;
            case CAFETERIA: localizedFnbType = "Cafeteria"; break;
            case COFFEE_HOUSE: localizedFnbType = "Coffee House"; break;
            case COFFEE_SHOP: localizedFnbType = "Coffee Shop"; break;
            case CASUAL_DINING: localizedFnbType = "Casual Dining"; break;
            case FAMILY_STYLE: localizedFnbType = "Family Style"; break;
            case FAST_CASUAL: localizedFnbType = "Fast Casual"; break;
            case FAST_FOOD: localizedFnbType = "Fast Food"; break;
            case FINE_DINING: localizedFnbType = "Fine Dining"; break;
            case FOOD_COURT: localizedFnbType = "Food Court"; break;
            case HAWKER_CENTRE: localizedFnbType = "Hawker Centre"; break;
            case PUB: localizedFnbType = "Pub"; break;
            case TEA_HOUSE: localizedFnbType = "Tea House"; break;
        }
        return localizedFnbType;
    }
    
    // singapore english (en_SG)
    private static String getFnbTypeAsString_EN_SG(FnbType fnbType) {
        String localizedFnbType = "";
        
        switch (fnbType) {
            case BISTRO: localizedFnbType = "Bistro" ; break;
            case BUFFET: localizedFnbType = "Buffet"; break;
            case CAFE: localizedFnbType = "Cafe"; break;
            case CAFETERIA: localizedFnbType = "Cafeteria"; break;
            case COFFEE_HOUSE: localizedFnbType = "Coffee House"; break;
            case COFFEE_SHOP: localizedFnbType = "Coffee Shop"; break;
            case CASUAL_DINING: localizedFnbType = "Casual Dining"; break;
            case FAMILY_STYLE: localizedFnbType = "Family Style"; break;
            case FAST_CASUAL: localizedFnbType = "Fast Casual"; break;
            case FAST_FOOD: localizedFnbType = "Fast Food"; break;
            case FINE_DINING: localizedFnbType = "Fine Dining"; break;
            case FOOD_COURT: localizedFnbType = "Food Court"; break;
            case HAWKER_CENTRE: localizedFnbType = "Hawker Centre"; break;
            case PUB: localizedFnbType = "Pub"; break;
            case TEA_HOUSE: localizedFnbType = "Tea House"; break;
        }
        return localizedFnbType;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getFnbTypeAsString_MS_MY(FnbType fnbType) {
        String localizedFnbType = "";
        
        switch (fnbType) {
            case BISTRO: localizedFnbType = "Bistro" ; break;
            case BUFFET: localizedFnbType = "Bufet"; break;
            case CAFE: localizedFnbType = "Cafe"; break;
            case CAFETERIA: localizedFnbType = "Cafeteria"; break;
            case COFFEE_HOUSE: localizedFnbType = "Rumah Coffee"; break;
            case COFFEE_SHOP: localizedFnbType = "Kedai Kopi"; break;
            case CASUAL_DINING: localizedFnbType = "Santapan Gaya Kasual"; break;
            case FAMILY_STYLE: localizedFnbType = "Gaya Keluarga"; break;
            case FAST_CASUAL: localizedFnbType = "Fast Kasual"; break;
            case FAST_FOOD: localizedFnbType = "Makanan Segera"; break;
            case FINE_DINING: localizedFnbType = "Santapan"; break;
            case FOOD_COURT: localizedFnbType = "Mahkamah Makanan"; break;
            case HAWKER_CENTRE: localizedFnbType = "Penjaja Pusat"; break;
            case PUB: localizedFnbType = "Pub"; break;
            case TEA_HOUSE: localizedFnbType = "Rumah Teh"; break;
        }
        return localizedFnbType;
    }
}